var _commseldivHTML = "<div id=\"commSelDiv\" class=\"comm_sel_div\">" +
                      "   <div id=\"commSelDivTitle\" class=\"comm_sel_div_title\">" +
                      "       <a class=\"abutton\" href=\"javascript:void(0);\" onClick=\"hideCommSelDiv();\">关闭</a>" +
                      "   </div>" +
                      "   <div id=\"commSelDivSearch\" class=\"comm_sel_div_search\"></div>" +
                      "   <div id=\"commSelDivSearchHint\" class=\"comm_sel_div_search_hint\">"+
                      "     提示:可输入首字母拼音查找,例:输入mx查找\"明星\""+
                      "   </div>" +
                      "   <div id=\"commSelDivContentZone\" class=\"comm_sel_div_contentzone\"><div id=\"commSelDivContent\" class=\"comm_sel_div_content\"></div></div>" +
                      "</div>";
var _commseldivsearchHTML = "输入关键字: <input name=\"commsel_key\" type=\"text\" id=\"commsel_key\" " +
                            "size=\"15\" maxlength=\"20\" " +
                            "onkeyup=\"doCommSelSearchRealTime();\"> " +
                            "<input name=\"submit_cs\" type=\"button\" class=\"BUTTON4\" value=\"搜索\" " +
                            "onClick=\"doCommSelSearch(true);\">";
var _commseldivCoverHTML = "<div id=\"commSelDivCover\" onclick=\"hideCommSelDiv();\"></div>";
var _Const_DivWidth = 0;
var _Const_DivHeight = 0;
var _Const_ObjectType = 0;
var _Const_ObjectID=0;
var _Cur_DataType = "";
var _Cur_Suffix = "";
var _Cur_E_Target=null;
var _Const_Sepechar="|";
var loadfinished = false;
document.write(_commseldivHTML);
document.write(_commseldivCoverHTML);


var commSelDiv,commSelDivTitle,commSelDivSearch,commSelDivContentZone,commSelDivContent,commSelDivCover;
var commSelKey;
function initCommSel(_objecttype,_objectid,_sepechar) {
    _Const_ObjectType = _objecttype;
    if(_objectid!=null) _Const_ObjectID=_objectid;
    if(_sepechar!=null) _Const_Sepechar=_sepechar;
    commSelDiv = document.getElementById("commSelDiv");
    commSelDivTitle = document.getElementById("commSelDivTitle");
    commSelDivSearch = document.getElementById("commSelDivSearch");
    commSelDivContentZone = document.getElementById("commSelDivContentZone");
    commSelDivContent = document.getElementById("commSelDivContent");
    commSelDivCover = document.getElementById("commSelDivCover");
    _Const_DivWidth = _isIE ? commSelDiv.clientWidth : commSelDiv.clientWidth;
    _Const_DivHeight = _isIE ? commSelDiv.clientHeight : commSelDiv.clientHeight;
    commSelDivCover.className = _isIE ? "comm_sel_div_cover" : "comm_sel_div_cover_ff";
    commSelDivCover.style.height=document.body.scrollHeight;
    commSelDivSearch.innerHTML = _commseldivsearchHTML;
    commSelKey = document.getElementById("commsel_key");
}

function _showDataDiv(e,_datatype,pos,idcount,suffix){ 
	loadfinished = false;
	if(suffix!=undefined) _Cur_Suffix = suffix;
    _Cur_E_Target=e;
    commSelKey.setAttribute("from","commdata");
    commSelKey.setAttribute("pos",pos==null?"":pos);
    if(idcount!=null&&idcount>0) commSelKey.setAttribute("idcount",idcount);
    else commSelKey.removeAttribute("idcount");
    showCommSelDiv(e, _datatype);
}
function showCommSelDiv(e, _datatype) {
    _Cur_DataType = _datatype;
    var e_divw = 0,e_divh = 0;
    try {
        e_divw = parseInt(e.getAttribute("divw"));
        e_divh = parseInt(e.getAttribute("divh"));
    } catch(e) {
    }
    commSelDivContent.innerHTML="";
    showCommSelDivCover(e);
    var t = e.offsetTop,  h = e.clientHeight, w = e.clientWidth, l = e.offsetLeft, p = e.type;
    while(e = e.offsetParent) {
        t += e.offsetTop+4;
        l += e.offsetLeft;
    }
    commSelDiv.style.width = e_divw > 0 ? e_divw : _Const_DivWidth;
    commSelDiv.style.height = e_divh > 0 ? e_divh : _Const_DivHeight;
    commSelDiv.style.zIndex = 100;
    commSelDiv.style.visibility = "visible";
    var pos=commSelKey.getAttribute("pos");
    if(pos==null||pos==''){
        commSelDiv.style.left = l;
        commSelDiv.style.top = t + h + 4;
    }else if(pos=="right"){
        commSelDiv.style.left = l+w+4;
        commSelDiv.style.top = t;
    }else if(pos=="top"){
        commSelDiv.style.left = l+w+4;
        commSelDiv.style.top = t-300;
    }
    commSelDivContentZone.style.height = (_isIE ? commSelDiv.style.pixelHeight : commSelDiv.clientHeight) - 64;
    loadInitData(_datatype);
}
function resetCommSelDivPos(){
    if(_Cur_E_Target==null) return;
    if(commSelDiv.style.visibility=="hidden") return;
    var e=_Cur_E_Target;
    var t = e.offsetTop,  h = e.clientHeight, w = e.clientWidth, l = e.offsetLeft, p = e.type;
    while(e = e.offsetParent) {
        t += e.offsetTop;
        l += e.offsetLeft;
    }
    var pos=commSelKey.getAttribute("pos");
    if(pos==null){
        commSelDiv.style.left = l;
        commSelDiv.style.top = t + h + 4;
    }else if(pos=="right"){
        commSelDiv.style.left = l+w+4;
        commSelDiv.style.top = t;
    }
}


/*------------------ People Select -----------------------*/

function _showPeopleDiv(e,_itemid,_quickadd){
    _Cur_E_Target=e;
    commSelKey.setAttribute("from","people");
    commSelKey.setAttribute("itemid",_itemid);
    if(_quickadd!=null) commSelKey.setAttribute("quickadd",_quickadd);
    else commSelKey.removeAttribute("quickadd");
    showPeopleSelDiv(e,_itemid);
}
function showPeopleSelDiv(e, _itemid) {
    var e_divw = 0,e_divh = 0;
    try {
        e_divw = parseInt(e.getAttribute("divw"));
        e_divh = parseInt(e.getAttribute("divh"));
    } catch(e) {
    }
    commSelDivContent.innerHTML="";
    showCommSelDivCover(e);
    var t = e.offsetTop,  h = e.clientHeight, w = e.clientWidth, l = e.offsetLeft, p = e.type;
    while(e = e.offsetParent) {
        t += e.offsetTop;
        l += e.offsetLeft;
    }
    commSelDiv.style.width = e_divw > 0 ? e_divw : _Const_DivWidth;
    commSelDiv.style.height = e_divh > 0 ? e_divh : _Const_DivHeight;
    commSelDiv.style.zIndex = 100;
    commSelDiv.style.visibility = "visible";
    commSelDiv.style.left = l+w+4;
    commSelDiv.style.top = t;
    commSelDivContentZone.style.height = (_isIE ? commSelDiv.style.pixelHeight : commSelDiv.clientHeight) - 45;
    loadInitPeopleData(_itemid);
}
function resetPeopleSelDivPos(){
    if(_Cur_E_Target==null) return;
    if(commSelDiv.style.visibility=="hidden") return;
    var e=_Cur_E_Target;
    var t = e.offsetTop,  h = e.clientHeight, w = e.clientWidth, l = e.offsetLeft, p = e.type;
    while(e = e.offsetParent) {
        t += e.offsetTop;
        l += e.offsetLeft;
    }
    commSelDiv.style.left = l+w+4;
    commSelDiv.style.top = t;
}

/*------------------ People Select End -----------------------*/


function _clearPeopleIds(itemid){
    var obj=document.getElementById("people_ids_"+itemid);
    if(obj!=null) obj.value='';
    var objname=document.getElementById("people_names_"+itemid);
    if(objname!=null) objname.innerHTML='';
}


function showCommSelDivCover(e) {
    document.body.style.width = "100%";
    document.body.style.height = "100%";
    //document.body.style.overflow = "hidden";
    commSelDivCover.style.visibility = "visible";
    if(commSelKey != null) commSelKey.value = "";
}
var cataChange = function(){}
function hideCommSelDiv() {
    if(cataChange) cataChange();
    _Cur_DataType = "";
    document.body.style.overflow = "auto";
    commSelDivCover.style.visibility = "hidden";
    commSelDiv.style.visibility = "hidden";
    clickCommData = clickCommData_bak;
}

function clearCommSelDatas(_datatype) {
    var _objids = document.getElementById("comm_ids_" + _datatype+_Cur_Suffix);
    var _objtexts = document.getElementById("comm_div_" + _datatype+_Cur_Suffix);
    if(_objids != null && _objtexts != null) {
        _objids.value = "";
        _objtexts.innerHTML = "";
    }
}


function loadInitData(datatype,params,method) {
	var _objids = document.getElementById("comm_ids_" + datatype+_Cur_Suffix);
    var _objidsstr=_objids!=null?_objids.value:"";
    if(datatype=='tag'){
        var _objidssys=document.getElementById("comm_ids_"+datatype+"_sys");
        if(_objidssys.value!='') _objidsstr=_objidssys.value+","+_objidsstr;
    }
    var url = "XML_CommDataPicker.jsp";
    if(datatype=='tag') url="XML_TagPicker.jsp";
    if(datatype=='add_bppcata' || datatype=='mod_bppcata' || datatype=='bppcata') url="XML_BppCataPicker.jsp";
    if(datatype.indexOf('catalog')>=0) url="XML_CatalogPicker.jsp";
    //体育公用电影的数据
    if((datatype=="language"||datatype=="color")&&_Const_ObjectType==9){
    	objecttype = 0;
    }
    //资讯的语言公用电影数据
    if((datatype=="language")&&_Const_ObjectType==10){
    	_Const_ObjectType = 0;
    }
    var pars = {objecttype:_Const_ObjectType,datatype:datatype,exist_dataids:_objidsstr,time:new Date().getTime()};
    if (params!=undefined) jQuery.extend(pars,params);
    if(method==null || method=="get"){
		$.get(url,pars,function(data){
	        commSelDivContent.innerHTML = data; 
		 });  
    }else{
    	$.post(url,pars,function(data){
	        commSelDivContent.innerHTML = data; 
		 }); 
    }
    loadfinished = true;
}


/*------------------ People Select -----------------------*/

function loadInitPeopleData(itemid,params,method) {
	var quickadd=commSelKey.getAttribute("quickadd");
    var _objids = document.getElementById("people_ids_" + itemid);
    var url = "XML_PeoplePicker.jsp";
    var pars = {objecttype:_Const_ObjectType,objectid:_Const_ObjectID,itemid:itemid,quickadd:quickadd?1:"",exist_dataids:_objids.value,
			time:new Date().getTime()};
    if (params!=undefined) jQuery.extend(pars,params);
    if(method==null || method=="get"){
		$.get(url,pars,function(data){
	        commSelDivContent.innerHTML = data; 
		});  
    }else{
    	$.post(url,pars,function(data){
	        commSelDivContent.innerHTML = data; 
		}); 
    }
}
/*------------------ People Select End -----------------------*/
function clickCommData(_did, _dtext, _datatype, _sepechar) {
    var _objids = document.getElementById("comm_ids_" + _datatype+_Cur_Suffix);
    var _objtexts = document.getElementById("comm_div_" + _datatype+_Cur_Suffix);
    if(_objids != null && _objtexts != null) {
        var _idcount=commSelKey.getAttribute("idcount");
        var _arrids=_objids.value.split(",");
        if(_idcount!=null&&_arrids!=null&&_objids.value!=''&&_arrids.length>=_idcount){
        	if(_idcount==1){
        		_objids.value = _did;
                //_objtexts.innerHTML = _dtext;
                $(_objtexts).html('<span style="vertical-align: bottom;">'+_dtext+'<img style="cursor: pointer;" src="images/comm/icondel.gif" onclick="delOneCata(this,'+_did+',\''+_datatype+'\')"></span>');
        	}else{
        		alert("最多只能选择"+_idcount+"个,已达到最大限制.");
        	}
            hideCommSelDiv();
            return;
        }
        if(existInArrayStr(_objids.value, _did)) return;
        _objids.value = addToArrayStr(_objids.value, _did);
        //_objtexts.innerHTML = addToArrayStr(_objtexts.innerHTML, _dtext, _sepechar!=null?_sepechar:_Const_Sepechar);
        $(_objtexts).append('<span style="vertical-align: bottom;">'+_dtext+'<img style="cursor: pointer;" src="images/comm/icondel.gif" onclick="delOneCata(this,'+_did+',\''+_datatype+'\')"></span>');
        _arrids=_objids.value.split(",");
        var _objxmlspan = document.getElementById("comm_sel_div_xml_span_" + _did);
        if(_objxmlspan != null) {
            _objxmlspan.innerHTML = _isIE ? _objxmlspan.innerText : _objxmlspan.textContent;
            //resetCommSelDivPos();
            if(_idcount!=null&&_arrids!=null&&_objids.value!=''&&_arrids.length>=_idcount){
                hideCommSelDiv();
            }
        }
    }  
}

/*-------------search------------------*/

function doCommSelSearchRealTime() {
    doCommSelSearch(true);
}

function doCommSelSearch(bRealTime) {
    var _from=commSelKey.getAttribute("from");
    if(commSelKey.value == '') {
        if(_from == "people") {
            loadInitPeopleData(commSelKey.getAttribute("itemid"));
        } else if(_from=="company"){
            loadInitObjectData(commSelKey.getAttribute("itemid"));
        } else{
            loadInitData(_Cur_DataType);
        }
        return;
    }
    var params = {key:commSelKey.value};
    if(bRealTime) jQuery.extend(params, {rt:1}); 
    if(_from=="commdata"){
        loadInitData(_Cur_DataType, params, "post");
    }else if(_from=="people"){
        loadInitPeopleData(commSelKey.getAttribute("itemid"),params,"post");
    }else if(_from=="company"){
        loadInitObjectData(commSelKey.getAttribute("itemid"),params,"post");
    }
}




/*
* ********************************Object Select***********************************
* *****************************
* *****************************Company
* *****************************Website
* *****************************
* ********************************************************************************
* */

function _showObjectDiv(e,_itemid,_from,_quickadd){
    _Cur_E_Target=e;
    commSelKey.setAttribute("from",_from);
    commSelKey.setAttribute("itemid",_itemid);
    if(_quickadd!=null) commSelKey.setAttribute("quickadd",_quickadd);
    else commSelKey.removeAttribute("quickadd");
    showObjectSelDiv(e,_itemid,_from);
}
function showObjectSelDiv(e, _itemid,_from) {
    var e_divw = 0,e_divh = 0;
    try {
        e_divw = parseInt(e.getAttribute("divw"));
        e_divh = parseInt(e.getAttribute("divh"));
    } catch(e) {
    }
    commSelDivContent.innerHTML="";
    showCommSelDivCover(e);
    var t = e.offsetTop,  h = e.clientHeight, w = e.clientWidth, l = e.offsetLeft, p = e.type;
    while(e = e.offsetParent) {
        t += e.offsetTop;
        l += e.offsetLeft;
    }
    commSelDiv.style.width = e_divw > 0 ? e_divw : _Const_DivWidth;
    commSelDiv.style.height = e_divh > 0 ? e_divh : _Const_DivHeight;
    commSelDiv.style.zIndex = 100;
    commSelDiv.style.visibility = "visible";
    commSelDiv.style.left = l+w+4;
    commSelDiv.style.top = t;
    commSelDivContentZone.style.height = (_isIE ? commSelDiv.style.pixelHeight : commSelDiv.clientHeight) - 45;
    loadInitObjectData(_itemid);
}
function resetObjectSelDivPos(){
    if(_Cur_E_Target==null) return;
    if(commSelDiv.style.visibility=="hidden") return;
    var e=_Cur_E_Target;
    var t = e.offsetTop,  h = e.clientHeight, w = e.clientWidth, l = e.offsetLeft, p = e.type;
    while(e = e.offsetParent) {
        t += e.offsetTop;
        l += e.offsetLeft;
    }
    commSelDiv.style.left = l+w+4;
    commSelDiv.style.top = t;
}

/*------------------ Object Select End -----------------------*/


/*------------------ Object Select -----------------------*/

function loadInitObjectData(itemid,params,method) {
	this.quickadd=commSelKey.getAttribute("quickadd");
    this.from = commSelKey.getAttribute("from");
    var _objids = document.getElementById(this.from+ "_ids_" + this.itemid);
    var url="XML_CompanyPicker.jsp";
    var pars = {itemid:itemid,quickadd:quickadd?1:"",exist_ids:_objids!=null?_objids.value:"",
			time:new Date().getTime()};
    if (params!=undefined) jQuery.extend(pars,params);
    if(method==null || method=="get"){
		$.get(url,pars,function(data){
	        commSelDivContent.innerHTML = data; 
		});  
    }else{
    	$.post(url,pars,function(data){
	        commSelDivContent.innerHTML = data; 
		}); 
    }
}

function displayAllCata(datatype,exist_dataids,method){
	var url;
	if(datatype=='add_bppcata' || datatype=='mod_bppcata' || datatype=='bppcata') url="XML_BppCataPicker.jsp";
    if(datatype.indexOf('catalog')>=0) url="XML_CatalogPicker.jsp";
	var pars = {objecttype:_Const_ObjectType,datatype:datatype,exist_dataids:exist_dataids,isall:1,time:new Date().getTime()};
    if(method==null || method=="get"){
		$.get(url,pars,function(data){
	        commSelDivContent.innerHTML = data; 
		 });  
    }else{
    	$.post(url,pars,function(data){
	        commSelDivContent.innerHTML = data; 
		 }); 
    }
    loadfinished = true;
}
function displayAllByParentID(id,datatype,exist_dataids,method){
	var pars = {id:id,objecttype:_Const_ObjectType,datatype:datatype,exist_dataids:exist_dataids,isall:1,time:new Date().getTime()};
    if(method==null || method=="get"){
		$.get("XML_CatalogMore.jsp",pars,function(data){
	        $("#cata_div_"+id).html(data);
		 });  
    }else{
    	$.post("XML_CatalogMore.jsp",pars,function(data){
    		$("#cata_div_"+id).html(data);
		 }); 
    }
    loadfinished = true;
}
function delOneCata(e,_id,_datatype){
	$(e).parent().remove();
	var ids = $("#comm_ids_" +_datatype+_Cur_Suffix).val();
	var newids = ids.indexOf(",")>0?ids.replace(_id+",","").replace(","+_id,""):"";
	$("#comm_ids_" +_datatype+_Cur_Suffix).val(newids);
}
var clickCommData_bak = clickCommData;

function selectCataToInput(e,catatype){
	clickCommData = function(_did, _dtext){
		var _objtexts = $("#s_cataname");
		var _objids = $("#comm_ids_"+catatype);	    
	    if(_objtexts != null && _objids != null) {
	        var _idcount=10;
	        var _arrids=_objids.val().split(",");
	        if(_idcount!=null&&_arrids!=null&&_objids.val()!=''&&_arrids.length>=_idcount){
	        	if(_idcount==1){
	        		_objids.val(_did);
	                _objtexts.val(_dtext);
	        	}else{
	        		alert("最多只能选择"+_idcount+"个,已达到最大限制.");
	        	}
	            hideCommSelDiv();
	            return;
	        }
	        if(existInArrayStr(_objids.val(), _did)) return;
	        _objids.val(addToArrayStr(_objids.val(), _did));
	        var lastindexsep = _dtext.lastIndexOf(">");
	        _objtexts.val(addToArrayStr(_objtexts.val(), _dtext.substr(lastindexsep>=0?lastindexsep+2:0), ","));
	        _arrids=_objids.val().split(",");
	        var _objxmlspan = document.getElementById("comm_sel_div_xml_span_" + _did);
	        if(_objxmlspan != null) {
	            _objxmlspan.innerHTML = _isIE ? _objxmlspan.innerText : _objxmlspan.textContent;
	            resetCommSelDivPos();
	            if(_idcount!=null&&_arrids!=null&&_objids.val()!=''&&_arrids.length>=_idcount){
	                hideCommSelDiv();
	            }
	        }
	    }
    }
    _showDataDiv(e,catatype,'right',1);
}
/*------------------ People Select End -----------------------*/

